package com.video4s.manager;

import org.apache.log4j.Logger;

import com.video4s.dao.UserYoutubeDAO;
import com.video4s.dao.UserYoutubeImplDAO;
import com.video4s.entity.UserYoutubeEntity;
import com.video4s.utils.PropetiesUtils;

/**
 * 
 * @author KhanhLV
 *
 */
public class ThreadQueueDBUser implements Runnable {
	private static UserYoutubeDAO userYoutubeDAO = new UserYoutubeImplDAO();
	private static Logger logger = Logger.getLogger(ThreadQueueDBUser.class);
	private static int SIZE_DB_USER = 100;
	private static int SLEEP_DB_USER = 10000;
	public ThreadQueueDBUser() {
		logger.info("## START THREAD DB USER ###############################################");
	}
	@Override
	public void run() {
		while(true) {
			try {
				SIZE_DB_USER = Integer.parseInt(PropetiesUtils.loadValue("SIZE_DB_USER"));
				SLEEP_DB_USER = Integer.parseInt(PropetiesUtils.loadValue("SLEEP_DB_USER"));
			} catch (Exception e) {
				logger.info("## ERROR LOAD CONFIG",e);
			}
			try {
				int queueSize = ManagerQueue.concurrentLinkedQueueUserYoutube.size() > SIZE_DB_USER ? SIZE_DB_USER : ManagerQueue.concurrentLinkedQueueUserYoutube.size();
				for (int i=0;i< queueSize;i++) {
					UserYoutubeEntity entity = ManagerQueue.concurrentLinkedQueueUserYoutube.poll();
					//logger.info("## INSERT USRER YOUTUBE: "+entity.getUserYoutubeLink());
					userYoutubeDAO.onInsert(entity);
				}
				Thread.sleep(SLEEP_DB_USER);
			} catch (Exception e) {
				logger.info("## ERROR INSERT USRER YOUTUBE: ",e);
			}
		}
	}

	
}
